package com.brayan.melinxa.logic.execution;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.inject.Inject;

import org.apache.log4j.Logger;

public class Copy implements Executable {
	@Inject 
	Logger logger;
	Path eventObject, parent, src = null, dest = null;	

	/**
	 * @param fileObject The object that has been touched.
	 * @param src The source root directory.
	 * @param dest The destination root directory.
	 */
	public Copy(Path fileObject, Path src, Path dest) {		
		this.eventObject = fileObject;
		/**The source root directory.*/
		this.src = src;
		/**The destination root directory. */
		this.dest = dest;
	}

	public void run() {

		String strObj = eventObject.toFile().getAbsolutePath();
		if (eventObject.toFile().isDirectory()) {
			
			// cut the common prefix of the path
			String strSrc = this.src.toString();
			String strDiff = strObj.replace((CharSequence)strSrc, "");			
			String copyDir = dest.toString() + strDiff;	
			
			//TODO use logger
			System.out.println("Copy new file item: " + copyDir);
			//logger.debug("Copy new file item: " + copyDir);
			
			try {
				Files.copy(eventObject, Paths.get(copyDir), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
				//TODO use logger: fine
				System.out.println("Successfully copied file/dir " + copyDir);
			} catch (IOException e) {
				// TODO USE LOGGER
				// No issue if it failes.
				System.out.println("Failed to copy the file. " + e.getMessage());				
			}	
		}
	}

	public String getSourcePath() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDestinationPath() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getExecutableType() {
		return "COPY";
	}
}
